package com.xiantm.study.thread.towparsetermination.p2;

public class CountupThread extends Thread {
    private long counter = 0;


    public void shutdownRequest(){
        interrupt();
    }


    @Override
    public void run() {
        try {
            while (!isInterrupted() ){
                    doWork();
            }
        } catch (InterruptedException ignored) { } finally {
            doShutDown();
        }
    }
//
    private void doWork() throws InterruptedException{
        counter++;
        System.out.println("doWork: counter = "+ counter);
        try {
            Thread.sleep(500);
        }catch (InterruptedException ignored){
            System.out.println("catch InterruptedException and handle.");
        }
    }

//    private void doWork() throws InterruptedException{
//        counter++;
//        System.out.println("doWork: counter = "+ counter);
//        Thread.sleep(500);
//    }

    private void doShutDown() {
        System.out.println("doShutdown: counter = " + counter);
    }
}
